﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.Framework;

namespace com.subb.WebFramework
{
    public class FW_MenuDAL
    {
        public List<FW_Menu> GetAllMenus(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.FW_Menu.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.MenuID);
                ttlCount = query.Count();
                List<FW_Menu> menulist = query.Skip(start).Take(limit).ToList();
                return menulist;
            }
        }

        public List<FW_Menu> GetAllMenus()
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.FW_Menu.OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.MenuID).AsQueryable();
                List<FW_Menu> menulist = query.ToList();
                return menulist;
            }
        }

        public List<FW_Menu> GetAllMenusByType(string menuType)
        {
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.FW_Menu.Where(c => c.MenuType.Equals(menuType, StringComparison.CurrentCultureIgnoreCase)).OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.MenuID).AsQueryable();
            List<FW_Menu> menulist = query.ToList();
            return menulist;
        }

        public List<FW_MemberMenu> GetAllMemberMenu(string gid, int start, int limit, out int ttlCount)
        {
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.FW_MemberMenu.Where(c => c.GID.Equals(gid, StringComparison.CurrentCultureIgnoreCase)).OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.MenuID);
            ttlCount = query.Count();
            List<FW_MemberMenu> menuList = query.Skip(start).Take(limit).ToList();
            return menuList;
        }

        public List<FW_MemberMenu> GetAllMemberMenu(string gid)
        {
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.FW_MemberMenu.Where(c => c.GID.Equals(gid, StringComparison.CurrentCultureIgnoreCase)).OrderByDescending(c => c.SortNum ?? 0).ThenBy(c => c.MenuID);
            List<FW_MemberMenu> menuList = query.ToList();
            return menuList;
        }

        public FW_Menu GetMenu(int menuId)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                FW_Menu menu = ctx.FW_Menu.SingleOrDefault(c => c.MenuID == menuId);
                return menu;
            }
        }

        public void CreateMenu(string menuName, string menuType, int? sortNum, string menuUrl)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                FW_Menu menu = new FW_Menu
                {
                    MenuName = menuName,
                    MenuType = menuType,
                    SortNum = sortNum,
                    MenuUrl = menuUrl
                };
                ctx.FW_Menu.AddObject(menu);
                ctx.SaveChanges();
            }
        }

        public bool UpdateMenu(int menuId, string menuName, string menuType, int? sortNum, string menuUrl)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                FW_Menu menu = ctx.FW_Menu.SingleOrDefault(c => c.MenuID == menuId);
                if (menu != null)
                {
                    menu.MenuName = menuName;
                    menu.MenuType = menuType;
                    menu.SortNum = sortNum;
                    menu.MenuUrl = menuUrl;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveMenu(int menuId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                FW_Menu menu = ctx.FW_Menu.SingleOrDefault(c => c.MenuID == menuId);
                if (menu != null)
                {
                    ctx.FW_Menu.DeleteObject(menu);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public void CreateMemberMenu(string gid, int menuId, int sortNum)
        {
            using (SugardbEntities ctx = new SugardbEntities())
            {
                FW_MemberMenu memMenu = new FW_MemberMenu
                {
                    GID = gid,
                    MenuID = menuId,
                    SortNum = sortNum
                };
                ctx.FW_MemberMenu.AddObject(memMenu);
                ctx.SaveChanges();
            }
        }

        public bool RemoveMemberMenu(string gid, int menuId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                FW_MemberMenu memMenu = ctx.FW_MemberMenu.SingleOrDefault(c => c.GID.Equals(gid, StringComparison.CurrentCultureIgnoreCase) && c.MenuID == menuId);
                if (memMenu != null)
                {
                    ctx.FW_MemberMenu.DeleteObject(memMenu);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }
}
